package com.sino.second.dao.hibernate;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sino.second.bean.TblZjpsfp;
import com.sino.second.dao.IZjpsfpDAO;
import com.sino.util.SysParameter;

public class ZjpsfpDAO extends HibernateDaoSupport implements IZjpsfpDAO {
	private static final Log log = LogFactory.getLog(ZjpsfpDAO.class);

	public void delZjpsfp(TblZjpsfp zjpsfp) {
		this.getHibernateTemplate().delete(zjpsfp);
		this.getHibernateTemplate().flush();

	}

	@SuppressWarnings("unchecked")
	public List<TblZjpsfp> listAllZjpsfp(String hqlWhere) {
		String hql = "from TblZjpsfp";
		if (!"".equals(hqlWhere) && hqlWhere.length() > 0) {
			hql = hql + " where " + hqlWhere;
		}
		return (List<TblZjpsfp>) this.getHibernateTemplate().find(hql);
	}

	@SuppressWarnings("unchecked")
	public List<TblZjpsfp> listZjpsfpByXmpsmcId(String xmpsmcId, String hqlWhere) {
		String hql = "from TblZjpsfp tzpf where tzpf.sysXmpsmc.id=?";
		if (!"".equals(hqlWhere) && hqlWhere.length() > 0) {
			hql = hql + " and " + hqlWhere;
		}
		if(!SysParameter.THIRDREVIEW.equals(xmpsmcId))
		{
			hql = hql + " order by tzpf.tblZhuanjiarukuxinxibiao.sysZhuanyema";
		}
		else{
			hql=hql+" order by tzpf.tblZhuanjiarukuxinxibiao.FBh";
		}
		return (List<TblZjpsfp>) this.getHibernateTemplate()
				.find(hql, xmpsmcId);
	}

	public int listAssignExpertCountByXmpsmcId(String xmpsmcId, String hqlWhere) {
		String hql = "select count(tzpf.tblZhuanjiarukuxinxibiao) from TblZjpsfp tzpf where tzpf.sysXmpsmc.id=?";
		if (!"".equals(hqlWhere) && hqlWhere.length() > 0) {
			hql = hql + " and " + hqlWhere;
		}
		return (Integer) this.getSession().createQuery(hql).setParameter(0,
				xmpsmcId).uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<TblZjpsfp> listZjpsfpByXmpsmcId(String xmpsmcId,
			String zhuanyeId, String hqlWhere) {
		String hql = "from TblZjpsfp tzpf where tzpf.sysXmpsmc.id=? and tzpf.tblZhuanjiarukuxinxibiao.sysZhuanyema.bm=?";
		if (!"".equals(hqlWhere) && hqlWhere.length() > 0) {
			hql = hql + " and " + hqlWhere;
		}
		return (List<TblZjpsfp>) this.getSession().createQuery(hql)
				.setParameter(0, xmpsmcId).setParameter(1, zhuanyeId).list();
	}

	public void saveZjpsfp(TblZjpsfp zjpsfp) {
		this.getHibernateTemplate().save(zjpsfp);
		this.getHibernateTemplate().flush();

	}

	public void updateZjpsfp(TblZjpsfp zjpsfp) {
		this.getHibernateTemplate().update(zjpsfp);

	}

	public Integer findZjpsfpCountByXmpsmcId(String xmpsmcId, String zhuanyeId,
			String hqlWhere) {
		String hql = "select count(*) from TblZjpsfp tzpf where tzpf.sysXmpsmc.id=?";
		if(!"".equals(zhuanyeId) && zhuanyeId.length() > 0)
		{
			hql=hql+" and tzpf.tblZhuanjiarukuxinxibiao.sysZhuanyema.bm=?";
			if (!"".equals(hqlWhere) && hqlWhere.length() > 0) {
				hql = hql + " and " + hqlWhere;
			}
			return (Integer) this.getSession().createQuery(hql)
			.setParameter(0, xmpsmcId).setParameter(1, zhuanyeId).uniqueResult();
		}
		if (!"".equals(hqlWhere) && hqlWhere.length() > 0) {
			hql = hql + " and " + hqlWhere;
		}
		return (Integer) this.getSession().createQuery(hql)
				.setParameter(0, xmpsmcId).uniqueResult();
	}
	
	public boolean isXmpsmcExpert(String xmpsmcId, Integer expertId, String hqlWhere) {
		String hql="from TblZjpsfp tzpf where tzpf.sysXmpsmc.id=? and tzpf.tblZhuanjiarukuxinxibiao.FBh=?";
		if (!"".equals(hqlWhere) && hqlWhere.length() > 0) {
			hql = hql + " and " + hqlWhere;
		}
		TblZjpsfp zpf=(TblZjpsfp)this.getSession().createQuery(hql).setParameter(0, xmpsmcId).setParameter(1, expertId).uniqueResult();
		if(null!=zpf)
		{
			return true;
		}
		else{
			return false;
		}
	}
}
